/*!
 * This file implements a form to add new databases in the application.
 * @author Mazzer Yannis, Calingarayar Martial
 * @file newdb.h
 */

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QComboBox>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QFrame>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QSpinBox>
#include <QtGui/QTextEdit>
#include <QMessageBox>
#include <QApplication>
#include <QDebug>
#include "newdb.h"
#include "../../liteconnector/liteconnector.h"

newdbDialog::newdbDialog(QWidget *parent) : QDialog(parent)
{
    this->draw();
}

newdbDialog::newdbDialog(newdbDialog *dialog,
			   QWidget *parent) : QDialog(parent)
{
    this->draw();
    int idx = this->dbTypeBox->findText(dialog->getDriver());
    if(idx != -1)
	this->dbTypeBox->setCurrentIndex(idx);
    this->dbHostLineEdit->setText(dialog->getHost());
    this->dbNameLineEdit->setText(dialog->getName());
    this->dbPortLineEdit->setValue(dialog->getPort().toInt());
    this->dbUserLineEdit->setText(dialog->getUser());
    this->dbNickLineEdit->setText(dialog->getNick());
    this->dbCommentTextEdit->setText(dialog->getComment());

}

newdbDialog::newdbDialog(const QString &type,
			 const QString &host,
			 const QString &name,
			 const QString &port,
			 const QString &user,
			 const QString &nick,
			 const QString &comm,
			 QWidget *parent) : QDialog(parent)
{
    this->draw();
    int idx = this->dbTypeBox->findText(type);
    if(idx != -1)
	this->dbTypeBox->setCurrentIndex(idx);
    this->dbHostLineEdit->setText(host);
    this->dbNameLineEdit->setText(name);
    this->dbPortLineEdit->setValue(port.toInt());
    this->dbUserLineEdit->setText(user);
    this->dbNickLineEdit->setText(nick);
    this->dbCommentTextEdit->setText(comm);
}

void newdbDialog::draw() {
	if (this->objectName().isEmpty())
		this->setObjectName(QString::fromUtf8("newdbDialog"));
	this->resize(593, 457);
	this->setContextMenuPolicy(Qt::NoContextMenu);
	this->setSizeGripEnabled(false);
	this->setWindowTitle(QApplication::translate("newdbDialog",
				"Dialog", 0, QApplication::UnicodeUTF8));
	this->setModal(false);
	buttonBox = new QDialogButtonBox(this);
	buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
	buttonBox->setGeometry(QRect(10, 420, 571, 32));
	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
	dbTypeBox = new QComboBox(this);
	dbTypeBox->setObjectName(QString::fromUtf8("dbTypeBox"));
	dbTypeBox->setGeometry(QRect(390, 20, 191, 21));
	dbTypeBox->setFocusPolicy(Qt::ClickFocus);
	dbTypeBox->clear();
	dbTypeBox->insertItems(0, QStringList()
		<< QApplication::translate("newdbDialog", "PostGreSQL", 0,
			QApplication::UnicodeUTF8)
		<< QApplication::translate("newdbDialog", "MySQL", 0,
			QApplication::UnicodeUTF8)
		<< QApplication::translate("newdbDialog", "SQLite", 0,
			QApplication::UnicodeUTF8)
	);
	dbHostLineEdit = new QLineEdit(this);
	dbHostLineEdit->setObjectName(QString::fromUtf8("dbHostLineEdit"));
	dbHostLineEdit->setGeometry(QRect(390, 60, 191, 23));
	dbHostLineEdit->setMouseTracking(false);
	dbHostLineEdit->setFocusPolicy(Qt::StrongFocus);
	dbNameLineEdit = new QLineEdit(this);
	dbNameLineEdit->setObjectName(QString::fromUtf8("dbNameLineEdit"));
	dbNameLineEdit->setGeometry(QRect(390, 100, 191, 23));
	dbPortLineEdit = new QSpinBox(this);
	dbPortLineEdit->setObjectName(QString::fromUtf8("dbPortLineEdit"));
	dbPortLineEdit->setGeometry(QRect(390, 140, 191, 23));
	dbPortLineEdit->setMaximum(65535);
	dbPortLineEdit->setValue(5432);
	dbUserLineEdit = new QLineEdit(this);
	dbUserLineEdit->setObjectName(QString::fromUtf8("dbUserLineEdit"));
	dbUserLineEdit->setGeometry(QRect(390, 180, 191, 23));
	dbPassLineEdit = new QLineEdit(this);
	dbPassLineEdit->setObjectName(QString::fromUtf8("dbPassLineEdit"));
	dbPassLineEdit->setGeometry(QRect(390, 220, 191, 23));
	dbPassLineEdit->setEchoMode(QLineEdit::Password);
	dbTypeLabel = new QLabel(this);
	dbTypeLabel->setObjectName(QString::fromUtf8("dbTypeLabel"));
	dbTypeLabel->setGeometry(QRect(140, 20, 101, 17));
	dbTypeLabel->setText(QApplication::translate("newdbDialog",
				"Database Type", 0, QApplication::UnicodeUTF8));
	dbHostLabel = new QLabel(this);
	dbHostLabel->setObjectName(QString::fromUtf8("dbHostLabel"));
	dbHostLabel->setGeometry(QRect(140, 60, 131, 17));
	dbHostLabel->setText(QApplication::translate("newdbDialog",
				"Database Hostname", 0, QApplication::UnicodeUTF8));
	dbNameLabel = new QLabel(this);
	dbNameLabel->setObjectName(QString::fromUtf8("dbNameLabel"));
	dbNameLabel->setGeometry(QRect(140, 100, 111, 17));
	dbNameLabel->setText(QApplication::translate("newdbDialog",
				"Database Name", 0, QApplication::UnicodeUTF8));
	dbPortLabel = new QLabel(this);
	dbPortLabel->setObjectName(QString::fromUtf8("dbPortLabel"));
	dbPortLabel->setGeometry(QRect(140, 140, 101, 17));
	dbPortLabel->setText(QApplication::translate("newdbDialog",
				"Database Port", 0, QApplication::UnicodeUTF8));
	dbUserLabel = new QLabel(this);
	dbUserLabel->setObjectName(QString::fromUtf8("dbUserLabel"));
	dbUserLabel->setGeometry(QRect(140, 180, 101, 17));
	dbUserLabel->setText(QApplication::translate("newdbDialog",
				"Database User", 0, QApplication::UnicodeUTF8));
	dbPassLabel = new QLabel(this);
	dbPassLabel->setObjectName(QString::fromUtf8("dbPassLabel"));
	dbPassLabel->setGeometry(QRect(140, 220, 131, 17));
	dbPassLabel->setText(QApplication::translate("newdbDialog",
				"Database Password", 0, QApplication::UnicodeUTF8));
	line = new QFrame(this);
	line->setObjectName(QString::fromUtf8("line"));
	line->setGeometry(QRect(240, 20, 141, 21));
	line->setFrameShape(QFrame::HLine);
	line->setFrameShadow(QFrame::Sunken);
	line_2 = new QFrame(this);
	line_2->setObjectName(QString::fromUtf8("line_2"));
	line_2->setGeometry(QRect(280, 60, 101, 21));
	line_2->setFrameShape(QFrame::HLine);
	line_2->setFrameShadow(QFrame::Sunken);
	line_3 = new QFrame(this);
	line_3->setObjectName(QString::fromUtf8("line_3"));
	line_3->setGeometry(QRect(250, 100, 131, 21));
	line_3->setFrameShape(QFrame::HLine);
	line_3->setFrameShadow(QFrame::Sunken);
	line_4 = new QFrame(this);
	line_4->setObjectName(QString::fromUtf8("line_4"));
	line_4->setGeometry(QRect(240, 140, 141, 20));
	line_4->setFrameShape(QFrame::HLine);
	line_4->setFrameShadow(QFrame::Sunken);
	line_5 = new QFrame(this);
	line_5->setObjectName(QString::fromUtf8("line_5"));
	line_5->setGeometry(QRect(240, 180, 141, 21));
	line_5->setFrameShape(QFrame::HLine);
	line_5->setFrameShadow(QFrame::Sunken);
	line_6 = new QFrame(this);
	line_6->setObjectName(QString::fromUtf8("line_6"));
	line_6->setGeometry(QRect(270, 220, 111, 21));
	line_6->setFrameShape(QFrame::HLine);
	line_6->setFrameShadow(QFrame::Sunken);
	line_7 = new QFrame(this);
	line_7->setObjectName(QString::fromUtf8("line_7"));
	line_7->setGeometry(QRect(120, 20, 20, 381));
	line_7->setFrameShape(QFrame::VLine);
	line_7->setFrameShadow(QFrame::Sunken);
	line_8 = new QFrame(this);
	line_8->setObjectName(QString::fromUtf8("line_8"));
	line_8->setGeometry(QRect(270, 300, 111, 21));
	line_8->setFrameShape(QFrame::HLine);
	line_8->setFrameShadow(QFrame::Sunken);
	dbNickLineEdit = new QLineEdit(this);
	dbNickLineEdit->setObjectName(QString::fromUtf8("dbNickLineEdit"));
	dbNickLineEdit->setGeometry(QRect(390, 260, 191, 23));
	dbNickLabel = new QLabel(this);
	dbNickLabel->setObjectName(QString::fromUtf8("dbNickLabel"));
	dbNickLabel->setGeometry(QRect(140, 260, 131, 17));
	dbNickLabel->setText(QApplication::translate("newdbDialog",
				"Database Nickname", 0, QApplication::UnicodeUTF8));
	dbCommentLabel = new QLabel(this);
	dbCommentLabel->setObjectName(QString::fromUtf8("dbCommentLabel"));
	dbCommentLabel->setGeometry(QRect(140, 300, 131, 17));
	dbCommentLabel->setText(QApplication::translate("newdbDialog",
				"Database Comment", 0, QApplication::UnicodeUTF8));
	dbCommentTextEdit = new QTextEdit(this);
	dbCommentTextEdit->setObjectName(QString::fromUtf8("dbCommentTextEdit"));
	dbCommentTextEdit->setGeometry(QRect(390, 300, 191, 101));
	line_9 = new QFrame(this);
	line_9->setObjectName(QString::fromUtf8("line_9"));
	line_9->setGeometry(QRect(270, 260, 111, 21));
	line_9->setFrameShape(QFrame::HLine);
	line_9->setFrameShadow(QFrame::Sunken);
	InfoTextEdit = new QTextEdit(this);
	InfoTextEdit->setObjectName(QString::fromUtf8("InfoTextEdit"));
	InfoTextEdit->setGeometry(QRect(10, 50, 111, 361));
	InfoTextEdit->setReadOnly(true);
	InfoLabel = new QLabel(this);
	InfoLabel->setObjectName(QString::fromUtf8("InfoLabel"));
	InfoLabel->setGeometry(QRect(10, 20, 111, 18));
	InfoLabel->setText(QApplication::translate("newdbDialog",
	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
	"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
	"p, li { white-space: pre-wrap; }\n"
	"</style></head><body style=\" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
	"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Information</p></body></html>",
	0, QApplication::UnicodeUTF8));

	QObject::connect(this->dbTypeBox, SIGNAL(activated(QString)), this, SLOT(changePort(QString)));
	QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
	this->show();
}

dbConnector * newdbDialog::setUpDatabase()
{
    bool ok;
    int portInt;
    QString portStr = this->dbPortLineEdit->text();
    portInt = portStr.toInt(&ok, 10);
    QString newDriver;
    if(this->dbTypeBox->currentText() == tr("PostGreSQL"))
	newDriver = "QPSQL";
    else if(this->dbTypeBox->currentText() == tr("MySQL"))
	    newDriver = "QMYSQL";
    else if(this->dbTypeBox->currentText() == tr("SQLite"))
	newDriver = "QSQLITE";

    dbConnector *newdb = new dbConnector();
    QSqlError err;
    err = newdb->addConnection(newDriver,
			 this->dbNameLineEdit->text(),
			 this->dbHostLineEdit->text(),
			 this->dbUserLineEdit->text(),
			 this->dbPassLineEdit->text(),
			 this->dbNickLineEdit->text(),
			 this->dbCommentTextEdit->toPlainText(),
			 portInt);
    newdb->setNickName(this->dbNickLineEdit->text());
    newdb->setComment(this->dbCommentTextEdit->toPlainText());
    if(err.isValid()) {
	QMessageBox::about(this,
			   tr("SQL Error"),
			   QString("Erreur:")+= err.text());
	return NULL;
    }
    else {
	return newdb;
    }
}

QString newdbDialog::getDriver()
{
    return this->dbTypeBox->currentText();
}

QString newdbDialog::getHost()
{
    return this->dbHostLineEdit->text();
}

QString newdbDialog::getName()
{
    return this->dbNameLineEdit->text();
}

QString newdbDialog::getPort()
{
    return this->dbPortLineEdit->text();
}

QString newdbDialog::getUser()
{
    return this->dbUserLineEdit->text();
}

QString newdbDialog::getNick()
{
    return this->dbNickLineEdit->text();
}

QString newdbDialog::getComment()
{
    return this->dbCommentTextEdit->toPlainText();
}


void newdbDialog::changePort(QString dbType)
{
    int port = 0;
    if(dbType == QString("PostGreSQL")) {
	port = 5432;
    }
    if(dbType == QString("MySQL")) {
	port = 3306;
    }
    if(dbType == QString("SQLite")) {
	port = 0;
    }
    this->dbPortLineEdit->setValue(port);
}
